//
//  YZLCarImageListViewController.m
//  Car
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLCarImageListViewController.h"
#import "HTTPTOOL.h"
#import "YZLCarImageListModel.h"
#import "YZLCarImageListCollectionViewCell.h"
#import "YZLCarImageViewController.h"

@interface YZLCarImageListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSMutableArray *CarImageListModelArr;
@property (nonatomic, copy) NSString *price;
@property (nonatomic, copy) NSString *dropPrice;
@property (nonatomic, strong) NSMutableArray *carImageArr;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;

@end

@implementation YZLCarImageListViewController

#pragma mark - 数据处理
- (void)dataHandle {
    
    NSString *str = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getImageListNew.php?cityId=64&id=%@&imageType=%@&type=1", self.sericeStr, self.typeStr];
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.price = [result objectForKey:@"price"];
        self.dropPrice = [result objectForKey:@"dropPrice"];
        NSArray *arr = [result objectForKey:@"categoryList"];
        NSDictionary *dic = [arr objectAtIndex:0];
        NSArray *arr1 = [dic objectForKey:@"imagesList"];
        self.CarImageListModelArr = [NSMutableArray array];
        self.carImageArr = [NSMutableArray array];
        for (NSDictionary *dic in arr1) {
            
            YZLCarImageListModel *model = [YZLCarImageListModel CarImageListModelWithDic:dic];
            [self.CarImageListModelArr addObject:model];
            [self.carImageArr addObject:dic];
        }
        
        [self.collection reloadData];
        [self.activityView stopAnimating];
        
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - 创建CollectionView
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake((self.view.frame.size.width - 41) / 3.f, 90);
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collection = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flow];
    [self.view addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.collection registerClass:[YZLCarImageListCollectionViewCell class] forCellWithReuseIdentifier:@"image"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.headStr;
    
    [self dataHandle];
    [self createCollectionView];
    //返回
    [self skipReturnWithTarget:self];
    
    //bar颜色
    [self navigationbarColor];
    
//    NSLog(@"号%@", self.navigationItem.leftBarButtonItem.target);
    
    /* 指示器 */
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityView.color = [UIColor lightGrayColor];
    self.activityView.center = self.view.center;
    [self.view addSubview:self.activityView];
    [self.activityView startAnimating];

    
}

#pragma mark - collectionView 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.CarImageListModelArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YZLCarImageListModel *model = [self.CarImageListModelArr objectAtIndex:indexPath.item];
    YZLCarImageListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"image" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

#pragma mark - Item的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YZLCarImageViewController *vc = [[YZLCarImageViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.imageArr = self.carImageArr;
    vc.price = self.price;
    vc.dropPrice = self.dropPrice;
    vc.itemNum = indexPath.item;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
